package crm.view.Custom;

import crm.dao.impl.CustomerSqlSeverImpl;
import crm.entity.Customer;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * 客户注册基本信息
 */
public class LoginBasicCustom implements ActionListener {
    JFrame frame = null;

    JLabel name = null;
    JLabel pwd = null;
    JLabel phone = null;
    JLabel address = null;
    JLabel credit = null;
    JLabel contactId = null;


    JTextField NameField = null;
    JTextField PwdField = null;
    JTextField PhoneField = null;
    JTextField AddressField = null;
    JTextField CreditField = null;


    JTextField ContactIdField = null;
    //提交按钮（新增一条数据）
    JButton btn = null;
    JButton btn2 = null;

    public static void main(String[] args) {
        new LoginBasicCustom();
    }

    public LoginBasicCustom() {
        init();
        frame.setBounds(680, 200, 600, 500);
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.setLayout(null);
        frame.setVisible(true);
    }

    public void init() {
        frame = new JFrame("注册基本信息");
        name = new JLabel("输入您的姓名:");
        pwd = new JLabel("修改密码:");
        phone = new JLabel("修改联系方式:");
        address = new JLabel("修改联系地址:");
        credit = new JLabel("修改信用度:");
        contactId = new JLabel("修改联系人编号:");
        NameField = new JTextField();
        PwdField = new JTextField();
        PhoneField = new JTextField();
        AddressField = new JTextField();
        CreditField = new JTextField();
        ContactIdField = new JTextField();
        btn = new JButton("提交");
        btn2 = new JButton("清空");
        Font font = new Font("楷体", 2, 20);
        btn.setFont(font);
        btn2.setFont(font);
        name.setFont(font);
        NameField.setFont(font);
        pwd.setFont(font);
        PwdField.setFont(font);
        phone.setFont(font);
        PhoneField.setFont(font);
        address.setFont(font);
        AddressField.setFont(font);
        credit.setFont(font);
        CreditField.setFont(font);
        contactId.setFont(font);
        ContactIdField.setFont(font);


        frame.add(name);
        frame.add(NameField);
        frame.add(pwd);
        frame.add(PwdField);
        frame.add(phone);
        frame.add(PhoneField);
        frame.add(address);
        frame.add(AddressField);
        frame.add(credit);
        frame.add(CreditField);

        frame.add(contactId);
        frame.add(ContactIdField);
        frame.add(btn);
        frame.add(btn2);

        name.setBounds(40, 50, 150, 40);
        NameField.setBounds(200, 50, 300, 40);
        pwd.setBounds(40, 100, 150, 40);
        PwdField.setBounds(200, 100, 300, 40);
        phone.setBounds(40, 150, 150, 40);
        PhoneField.setBounds(200, 150, 300, 40);
        address.setBounds(40, 200, 150, 40);
        AddressField.setBounds(200, 200, 300, 40);
        contactId.setBounds(40, 250, 150, 40);
        ContactIdField.setBounds(200, 250, 300, 40);

        btn.setBounds(150, 310, 80, 30);
        btn2.setBounds(270, 310, 80, 30);

        btn.addActionListener(this);
        btn2.addActionListener(this);
    }


    @Override
    public void actionPerformed(ActionEvent e) {

        if (e.getSource() == btn) {
            if ("".equals(e.getActionCommand())) {
                JOptionPane.showMessageDialog(null, "请输入姓名！");

            } else {
                CustomerSqlSeverImpl dao = new CustomerSqlSeverImpl();
                Customer customer = new Customer();
                customer.setCustomer_name(NameField.getText());
                customer.setCustomer_pwd(PwdField.getText());
                customer.setCustomer_phone(PhoneField.getText());
                customer.setCustomer_address(PwdField.getText());
                customer.setCredit(CreditField.getText());
                customer.setContact_id(ContactIdField.getText());

                int add = dao.Update(customer);
                System.out.println(add);
                if (add > 0) {
                    JOptionPane.showMessageDialog(null, "提交成功！");
                }
            }

        } else if ("清空".equals(e.getActionCommand())) {
            NameField.setText("");
            PwdField.setText("");
            PhoneField.setText("");
            AddressField.setText("");
            CreditField.setText("");
            ContactIdField.setText("");

        }


    }
}
